get_cost_data_all  <- function( ) {

  ## add death date and index date to cost dt ------------------------------------
  temp <- data.table::copy(dt_original_fir[S_sample_source_XX == 'all_data' &
                                                    (S_index_date_XX != DMG_date_of_death_XX |
                                                       is.na(DMG_date_of_death_XX))])
  
  # convert S_index_date_XX to be of class Date for future merges
  temp[, S_index_date_XX := as.Date(fasttime::fastPOSIXct(S_index_date_XX))]
  
  # check that dt_org_plus is identified by the trio {ID, index_date, sample}
  if (nrow(unique(temp[, 
                              .(id_var, 
                                S_index_date_XX, 
                                S_sample_source_XX)])) != nrow(temp)) {
    stop("Problem with identifing unique observation in temp")
  }
  
  
    
time1 <- Sys.time()
dt_original_3rd_all <- data.table::as.data.table(
  DBI::dbGetQuery(
    con,
    paste0("sql_query"
    )
  )
)
time2 <- Sys.time()
print("Third table extraction took (in mins):")
print(time2-time1)
rm(time2, time1)
print("Third table extraction finished.")

dt_cost_by_mainCat_2y_all <-
  rbind(
    dt_original_3rd_all
  ) 
dt_cost_by_mainCat_2y_all[, main_cat := factor(main_cat)]
dt_cost_by_mainCat_2y_all[, category := factor(category)]
dt_cost_by_mainCat_2y_all[, profession := factor(profession)]
dt_cost_by_mainCat_2y_all[, cost_date := 
                            as.Date(fasttime::fastPOSIXct(
                              paste(as.character(event_date_year),
                                    as.character(event_date_month),
                                    as.character(event_date_day),
                                    sep = "-")
                            ))]
# due to some problems in the data base, sometimes event_length_days gets negative 
# value which isn't possible. Thus is changed to 0 
dt_cost_by_mainCat_2y_all[event_length_days<0, event_length_days := 0]
dt_cost_by_mainCat_2y_all[, event_date_end := cost_date+event_length_days]
dt_cost_by_mainCat_2y_all[, S_index_date_XX := 
                            as.Date(fasttime::fastPOSIXct(
                              paste(as.character(S_index_date_XX_year),
                                    as.character(S_index_date_XX_month),
                                    as.character(S_index_date_XX_day),
                                    sep = "-")
                            ))]


dt_cost_final_all <- merge(
  x = temp[S_sample_source_XX %in% c("all_data"),   
                  .(id_var = as.integer(id_var) ,
                    S_index_date_XX, S_sample_source_XX,
                    DMG_date_of_death_XX, DMG_died_within_365d)],
  y = dt_cost_by_mainCat_2y_all[, .(id_var = as.integer(id_var) ,
                                    S_index_date_XX, category, 
                                    profession,main_cat, amount, actual_cost, 
                                    cost_date, event_date_end)],
  by = c("id_var", "S_index_date_XX"),
  all.x = T,
  all.y = F
)

dt_cost_final_all[, DMG_date_of_death_XX := 
                    as.Date(fasttime::fastPOSIXct(DMG_date_of_death_XX))]
dt_cost_final_all[, DMG_died_within_365d := factor(DMG_died_within_365d)]


return(dt_cost_final_all) 
}